import $service from '../../../../service.js';
var that;
var type;
const app = getApp();
Page({
    data: {
        type:1,
        img: '../../../../static/images/head.png',
        user_nickname: '',
        user_document_number: '',
        user_document_type:'01',
        user_document_index: 0,
        user_document_name:'居民身份证',
        date:'请选择出生日期',
        certificateslist:[
            {'certificates_name':'居民身份证','id': '01'},
            {'certificates_name':'居民户口簿','id': '02'},
            {'certificates_name':'护照','id': '03'},
            {'certificates_name':'军官证','id': '04'},
            {'certificates_name':'驾驶证','id': '05'},
            {'certificates_name':'港澳居民来往内地通行证','id': '06'},
            {'certificates_name':'台湾居民来往内地通行证','id': '07'},
            {'certificates_name':'其他','id': '99'}
        ],
        disabled:false,
        userInfo: {},
        oosParams: {}
    },
    onLoad(e) {
        type = e.type;
        this.setData({
            type : type
        })
    },
    onShow() {
        that = this.data;
        this.setData({
            type: JSON.parse($service.getStore('userInfo')).type
        })
        that.type === 'D' ?  this.getDoctorInfo() : this.getUserInfo()
        this.ossPolicy();
    },
    // 获取个人信息
    getUserInfo() {
        app.http.getUserInfo().then(res => {
            let userInfo = $service.deepClone(res.data);
            // userInfo.userImg = userInfo.userImg ? userInfo.userImg : '../../../../static/images/head.png'
            $service.setStore('userData', JSON.stringify(userInfo))
            this.setData({
                userInfo
            })
        })
    },
    // 个人更新信息
    updateUserInfo() {
        let params = {
            "trueName": that.userInfo.trueName,
            "userDocumentNumber": that.userInfo.userDocumentNumber,
            "userNickname": that.userInfo.userNickname
          }
        app.http.updateUserInfo(params).then(res => {
            $service.showToast(res.msg)
            this.getUserInfo();
            setTimeout(() => {
                $service.openPage('/pages/user_side/personal/personal')
            },1000)
        }).catch(err => {
            $service.showToast(err.msg)
        })
    },
    // 输入框
    inputChange(e) {
        var name = e.detail.value;
        var index = e.currentTarget.dataset.index;
        if(index == 1){
            that.userInfo.userNickname = name
        }else if(index == 2){
            that.userInfo.trueName = name
        }else if(index == 3){
            that.userInfo.userDocumentNumber = name
        }
        this.setData({
           'userInfo.userNickname': that.userInfo.userNickname,
           'userInfo.trueName': that.userInfo.trueName,
           'userInfo.userDocumentNumber': that.userInfo.userDocumentNumber
        })
    },
    // 保存
    bcBtn() {
        if (that.type !== 'D') {
            if (!that.userInfo.userNickname) {
                $service.showToast('请输入昵称');
                return
            }
            if (!that.userInfo.trueName) {
                $service.showToast('请输入姓名');
                return
            }
            if (!$service.isEsgin) {
              if (!that.userInfo.userDocumentNumber) {
                  $service.showToast('请输入身份证号码');
                  return
              }
              if (!$service.validateElement('idCard', that.userInfo.userDocumentNumber)){
                  $service.showToast('请输入正确的身份证号码');
                  return
              }
            }
            this.updateUserInfo();
        }
    },
    //获取医生信息
    getDoctorInfo() {
        app.http.getDoctorInfo().then(res => {
            let userInfo = $service.deepClone(res.data);
            userInfo.userImg = userInfo.doctorHeadImg ? userInfo.doctorHeadImg : '../../../../static/images/head.png'
            $service.setStore('doctorInfo', JSON.stringify(userInfo))
            this.setData({
                userInfo
            })
        })
    },
    // oss信息
    ossPolicy() {
        app.http.ossPolicy().then(res => {
            this.setData({
                oosParams: res.data
            })
        })
    },
     // 上传图片
    uploadImg: function(event) {
        wx.chooseImage({
            count: 1,
            success: (res) => {
                const tempFilePaths = res.tempFilePaths;
                wx.uploadFile({
                    url: that.oosParams.host, 
                    filePath: tempFilePaths[0],
                    name: 'file',
                    formData: {
                        name:  $service.randomFilename(tempFilePaths[0]),
                        key: that.oosParams.dir +  $service.randomFilename(tempFilePaths[0]),
                        dir: that.oosParams.dir,
                        policy: that.oosParams.policy,
                        OSSAccessKeyId: that.oosParams.accessid,
                        success_action_status: "200", //让服务端返回200,不然，默认会返回204
                        callback: that.oosParams.callback,
                        signature: that.oosParams.signature
                        },
                    success: (res) => {
                        let {code, data} = JSON.parse(res.data)
                        console.log(data)
                        if (code === 1) {
                            this.setData({
                                'userInfo.userImg': data.filename
                            })
                            that.type === 'D' ?  this.updateDoctorHeadImg() : this.updateUserHeadImg();
                        }
                    },
                    fail:() => {
                        $service.showToast('上传失败')
                    }
                });
            },
        })
    },
    // 个人更新头像
    updateUserHeadImg() {
        app.http.updateUserHeadImg({userImg: that.userInfo.userImg}).then(res => {
            $service.showToast(res.msg)
            this.getUserInfo();
        }).catch(err => {
            $service.showToast(err.msg)
        })
    },
    // 医生更新头像
    updateDoctorHeadImg() {
        app.http.updateDoctorHeadImg({doctorHeadImg: that.userInfo.userImg}).then(res => {
            $service.showToast(res.msg)
            this.getDoctorInfo();
        }).catch(err => {
            $service.showToast(err.msg)
        })
    },
})
